﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SimpleRest.Core.Routing;

namespace SimpleRest.Core
{
    /// <summary>
    /// Event args fired when the application requests an initialization of settings.  This event args class
    /// allows the consumer to wire up DI (IoC container), HttpConfiguration object, and decide which assembilies
    /// he wants to scan for services to be registered.
    /// </summary>
    public class ApplicationExecutionEventArgs : EventArgs
    {
        public bool IsDefaultErrorHandlerEnabled { get; set; }
        public bool IsCorrelationHandlerEnabled { get; set; }
        public bool IsHttpMethodOverrideHandlerEnabled { get; set; }

        internal List<System.Reflection.Assembly> Assembilies;
        internal Dictionary<Type, Type> TypesToRegister = new Dictionary<Type, Type>();
        internal Dictionary<Type, Configuration> ServicesToRegister = new Dictionary<Type,Configuration>();

        public ApplicationExecutionEventArgs()
        {
        }

        /// <summary>
        /// Register type for IoC container for AutoFac.  Any types you want DI to wire up for you be sure and include them here.
        /// </summary>
        /// <typeparam name="XClass">The class implementing the interface.</typeparam>
        /// <typeparam name="TInterface">The interface implemented by the class.</typeparam>
        //public void RegisterType<XClass, TInterface>()
        //{
        //    //this.Builder.RegisterType<XClass>().As<TInterface>().SingleInstance();
        //    this.TypesToRegister.Add(typeof(XClass), typeof(TInterface));
        //}

        internal Configuration ScanAndRegisterConfiguration { get; set; }
            
        /// <summary>
        /// List the assembilies you wish to scan for service apis.  If this is not set, there will be no auto discovery of
        /// services and you will have to wire services up manually.  You will also lose the ability for the discovery service
        /// to know about your services.
        /// </summary>
        /// <param name="assembilies"></param>
        public void ScanAndRegisterAssembilies(Configuration configuration, params System.Reflection.Assembly[] assembilies)
        {
            this.ScanAndRegisterConfiguration = configuration;

            if (assembilies != null)
                Assembilies = assembilies.ToList();
        }

        public void RegisterService(Type type, Configuration configuration)
        {
            if (configuration == null)
                throw new ArgumentException("Configuration must be set in order to register service correctly.");
            if (ServicesToRegister.ContainsKey(type))
                throw new ArgumentException("This service has already been queued for registration.");

            ServicesToRegister.Add(type, configuration);
        }
    }
}
